package com.wu.foodsafetyplayer.widget;

import android.content.Context;
import android.util.AttributeSet;
import android.widget.ImageView;
import android.widget.ViewFlipper;

import com.nostra13.universalimageloader.core.ImageLoader;

import java.util.List;

/**
 * add by 玉念聿辉 20200429
 */
public class ImgPlayerViewFlipper extends ViewFlipper {
	private Context context;
	private int milliseconds = 5;// 切换速度（s）

	public ImgPlayerViewFlipper(Context context) {
		super(context);
		this.context = context;
	}

	public ImgPlayerViewFlipper(Context context, AttributeSet attrs) {
		super(context, attrs);
		this.context = context;
	}

	public void initMilliseconds(int milliseconds) {
		// TODO Auto-generated method stub
		this.milliseconds = milliseconds;
	}

	public void initImg(List<String> imgUrls) {
		// TODO Auto-generated method stub
		if (imgUrls == null) {
			return;
		}

		for (String url : imgUrls) {
			addImg(url);
		}
	}

	@Override
	protected void onAttachedToWindow() {
		super.onAttachedToWindow();
		// addImg(R.drawable.ic_live_adv);
		// addImg(R.drawable.ic_yisheng);

		setFlipInterval(milliseconds * 1000);
		setAutoStart(true);
		if (isAutoStart() && !isFlipping()) {
			startFlipping();
		}
	}

	/**
	 * 加载网络图片
	 * 
	 * @param url
	 */
	private void addImg(String url) {
		// TODO Auto-generated method stub
		ImageView iv = new ImageView(context);
		//iv.setScaleType(ImageView.ScaleType.FIT_XY);
		ImageLoader.getInstance().displayImage(url, iv);
		addView(iv);
	}

	/**
	 * 加载本地图片
	 * 
	 * @param img
	 */
	private void addImg(int img) {
		// TODO Auto-generated method stub
		ImageView iv = new ImageView(context);
		iv.setScaleType(ImageView.ScaleType.FIT_XY);
		iv.setImageResource(img);
		addView(iv);
	}

	@Override
	protected void onDetachedFromWindow() {
		super.onDetachedFromWindow();
	}

}
